const API = require('../../http/api')

Page({
  data: {
    searchVal: '', //搜索的关键词
    ind: 0, // 从首页点击进来的那个ID，初始值为零
    isShow: 1,
    a: [],
    goodsPrice: 0,
    activeKey: 0,
    goodslist: [], // 左边的数据
    goosListData: [], // 右边的数据
    id: 0,
    num: 1, // 数量
    show: false, //控制弹框显示
    properties: [], //商品sku属性列表
    selected: [], //选中商品的属性
    lunbo: [], //sku数据
    arrlist: {}, //价格的变化
  },

  // 去搜索页,携带关键词过去
  goSearch(e) {
    this.data.searchVal = e.detail.value
    // console.log(this.data.searchVal);
    this.setData({
      searchVal: e.detail.value,
    })
    wx.navigateTo({
      url: '../shangPinList/shangPinList?searchVal=' + this.data.searchVal,
    })

  },

  //  点击切换左侧
  onChange(e) {
    console.log('1', this.data.goodslist[e.detail].id);
    this.getyoulist({
      categoryId: this.data.goodslist[e.detail].id
    })
  },

  // 渲染左侧列表区
  async getlist() {
    const res = await API.getlist()
    console.log('上装', res);
    this.setData({
      goodslist: res.data
    })
  },
  // 渲染右侧 getgslist
  async getyoulist(obj) {
    const res = await API.getgslist(obj)
    console.log('上装右', res);
    let {
      goosListData
    } = this.data
    if (res.code == 0) {
      goosListData = res.data.result
    } else if (res.code == 700) {
      goosListData = []
    }
    this.setData({
      goosListData
    })
    console.log('右侧id', this.data.goosListData);
  },
  // 没有sku加入购物车
  async addShopCar1(item) {
    console.log('item', item);
    let obj = {
      goodsId: item.currentTarget.dataset.id,
      number: this.data.num, //商品数量
      token: wx.getStorageSync('token'),
    }
    console.log('ll', obj);
    const res = await API.addgoodslist(obj)
    if (res.code == 0) {
      wx.showToast({
        title: '添加购物车成功哦',
        icon: 'success'
      })
      console.log('加入购物车', res);
    }
  },

  // 有sku
  async addShopCar(item) {
    wx.hideTabBar({
      animation: true,
    })
    console.log('弹框显示');
    this.setData({
      show: true
    })
    console.log('item-id', item.currentTarget.dataset.id);
    const res = await API.getgoodszqlist(item.currentTarget.dataset.id)
    console.log('cate-xq', res);
    if (res.code == 0) {
      this.setData({
        lunbo: res.data
      })
    }
  },

  // 加入购物车
  async add() {
    let arr = Object.keys(this.data.selected);
    console.log('这是选中的sku', arr);
    //没有选择任何属性，
    console.log('bbb', this.data.lunbo.properties);
    if (arr.length != this.data.lunbo.properties.length) {
      wx.showToast({
        title: '请选择属性',
        icon: 'error'
      })
      return false;
    }
    //登录鉴权的功能，用户没有登录的话，提示用户，直接跳转到登录页面去
    if (wx.getStorageSync('token') == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'error'
      })
      wx.navigateTo({
        url: '/pages/my/my',
      })
    }
    //加入购物车效果
    let sku = this.data.selected.map((pid, id) => {
      return {
        optionId: id,
        optionValueId: pid
      }
    })
    console.log('skuu', sku);
    let arrs = []
    sku.forEach(item => {
      arrs.push(item)
    })
    const ggg = JSON.stringify(arrs)
    console.log('sss', ggg);
    let obj = {
      goodsId: this.data.lunbo.basicInfo.id,
      number: this.data.num, //商品数量
      token: wx.getStorageSync('token'),
      sku: ggg
    }
    const res = await API.addgoodslist(obj)
    if (res.code == 0) {
      wx.showToast({
        title: '添加购物车成功哦',
        icon: 'success'
      })
      wx.showTabBar({
        animation: true,
      })
      this.setData({
        show: false
      })
      console.log('加入购物车', res);
    }
    console.log('加入购物车', res);
  },
  
  // 计算数值发生改变
  onChange1(e) {
    // console.log('num', e);
    this.setData({
      num: e.detail
    })
  },

  async sel(e) {
    console.log('sku', e.currentTarget.dataset.propertyid, e.currentTarget.dataset.id);
    let {
      selected,
      arrlist
    } = this.data
    const pid = e.currentTarget.dataset.propertyid
    const id = e.currentTarget.dataset.id
    //设置选中的颜色
    selected[pid] = id;
    arrlist[pid] = id;
    this.setData({
      selected,
      arrlist,
    })
    let propertyChildIds = "";
    for (var key in this.data.arrlist) {
      //id值的拼接效果
      propertyChildIds += key + ":" + this.data.arrlist[key] + ",";
    }
    console.log('arrlist', propertyChildIds);
    let obj = {
      goodsId: this.data.lunbo.basicInfo.id,
      token: wx.getStorageSync('token'),
      propertyChildIds: propertyChildIds
    }
    const res = await API.getgoodslistjg(obj)
    console.log('价格', res);
    if (res.code == 0) {
      console.log(res.data.price);
      this.setData({
        goodsPrice: res.data.price
      })
    }
  },

  // 关闭弹框
  onClose() {
    this.setData({
      show: false,
      selected: [],
      goodsPrice: 0
    })
    wx.showTabBar({
      animation: true,
    })
  },

  // 一进页面显示
  onLoad(options) {
    console.log('首页ID', options.id, options.index);
    if (!options.id) {
      this.getyoulist({
        categoryId: this.data.id
      })
      return false
    }
    this.setData({
      activeKey: options.index,
      id: options.id
    })
  },

  onShow() {
    this.getlist()
    this.getyoulist({
      categoryId: this.data.id
    })
  },

  // 点击进入详情
  goxq(e){
    wx.navigateTo({
      url: `/pages/goodsInfo/goodsInfo?id=${e.currentTarget.dataset.id}`,
    })
  }


})